<template>
  <div v-if="isGetData">
    <Template1 :area-info="areaInfo" :website="websiteInfo" :nav="navInfo" :allCategoryList="allCategoryList" :policyList="policyList"/>
  </div>
</template>
<script lang="ts" setup>
import Template1 from "./template/template1.vue"
import {useRoute} from "vue-router";
import {getWebSite, getWebNav, getWebSiteProduct, getWebSiteCategory,getPolicyByAreaId} from "@/api/h5/web";
import {useAppStore} from "@/store/modules/app";
import {deepCopy, throttle} from "@/utils";

import {useMessage} from "@/hooks/web/useMessage";
import {getAreaById} from "@/api/h5";


const message = useMessage();

const isGetData = ref(false);

const allCategoryList = ref([])

const initQueryParams = {
  pageSize: 10,
  pageNo: 1
}

const queryParams = ref({
  ...initQueryParams
})


const pId = ref("");

const adId = ref("");

const areaInfo=ref({})

const websiteInfo = ref({})

const navInfo = ref([])

const policyList=ref([])


const initPage = async () => {

  let url = window.location.href;

  let urlStr = url.split('#')[0];
  let paramsStr = urlStr.split('?')[1];
  if (paramsStr) {
    let paramsArr = paramsStr.split("&");
    let params = {}
    paramsArr.forEach(item => {
      let keyVal = item.split("=");
      params[keyVal[0]] = keyVal[1]
    })
    if (params.utm_term) {
      adId.value = params.utm_term
    }

  }


  let callback = await getWebSite({
    domain:"shop.dashubobo.com"//window.location.hostname//"shop.dashubobo.com",//import.meta.env.IS_ORIGIN ?  : // window.location.hostname,//先暂时写死
  })
  websiteInfo.value = callback;
  useAppStore().setTenantId(callback?.tenantId)

  //更换ico
  let dom = document.getElementById('ico');
  dom.href = callback.webIcon
  await initPromise();

}

const initPromise = async () => {
  //获取导航

  let res = await Promise.all([getWebNav({webSiteId: websiteInfo.value.id}),    getPolicyByAreaId({areaId: websiteInfo.value.areaId}),    getAreaById({
    id: websiteInfo.value?.areaId
  }),])
  let nav = res[0];
  navInfo.value = nav

  let categoryArr = [];
  //获取所有的category

  let lis = [];
  generateCategoryList(nav, lis);


  for (let i = 0; i < lis.length; i++) {
    let item = lis[i]
    if (item.type == 1) {

      if (item.categoryIds && item.categoryIds.length > 0) {


        item.categoryIds.forEach(at => {

          let isIn = categoryArr.find(v => {
            return v == at
          })

          if (!isIn) {
            categoryArr.push(at)
          }
        })
      }
    }
  }

  let callback = await  getWebSiteCategory(categoryArr);
  allCategoryList.value = callback||[];

  let polis = res[1];

  if(polis && polis.length>0){
    policyList.value = polis[0].details?.filter(item=>item.type == 1)
  }

  areaInfo.value = res[2]

  isGetData.value = true;
}

const generateCategoryList = (list, lis) => {
  for (let i = 0; i < list.length; i++) {
    if (list[i].children) {
      generateCategoryList(list[i].children, lis);
    } else {
      lis.push(list[i]);
    }
  }
}


const handleBtnClick = (e) => {
  let type = e.type;
  try {
    fn[type](e)
  } catch (e) {
    console.log(e)
    console.log(`${type}不存在`)
  }
}

const fn = {
  async store({data, callback}) {
    //
    let res = await getStoreList({
      detailStateId: data.regionName,
      methodId: logisticsList.value[data.activeLogistics].id
    })
    storeList.value = res
    callback && callback(res);
  },
}


onMounted(() => {
  initPage();

})


</script>
<style lang="scss">

.el-select-dropdown__wrap {
  max-height: 250PX;

  .el-select-dropdown__item {

    height: 30PX !important;
    display: flex;
    align-items: center;
    align-content: center;

    span {
      font-size: 10PX;
    }

  }


}


</style>
